package com.boolib.animate.commons.utils;

import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @author nier
 * @create 2020-02-20 16:58
 */

public class ImageFileUtil {


    enum FileType {

        /**
         * JPEG
         */
        JPEG("FFD8FF", "jpg"),

        /**
         * PNG
         */
        PNG("89504E47", "png"),

        /**
         * GIF
         */
        GIF("47494638", "gif"),

        /**
         * TIFF
         */
        TIFF("49492A00"),

        /**
         * Windows bitmap
         */
        BMP("424D"),

        /**
         * CAD
         */
        DWG("41433130"),

        /**
         * Adobe photoshop
         */
        PSD("38425053"),

        /**
         * Rich Text Format
         */
        RTF("7B5C727466"),

        /**
         * XML
         */
        XML("3C3F786D6C"),

        /**
         * HTML
         */
        HTML("68746D6C3E"),

        /**
         * Outlook Express
         */
        DBX("CFAD12FEC5FD746F "),

        /**
         * Outlook
         */
        PST("2142444E"),

        /**
         * doc;xls;dot;ppt;xla;ppa;pps;pot;msi;sdw;db
         */
        OLE2("0xD0CF11E0A1B11AE1"),

        /**
         * Microsoft Word/Excel
         */
        XLS_DOC("D0CF11E0"),

        /**
         * Microsoft Access
         */
        MDB("5374616E64617264204A"),

        /**
         * Word Perfect
         */
        WPB("FF575043"),

        /**
         * Postscript
         */
        EPS_PS("252150532D41646F6265"),

        /**
         * Adobe Acrobat
         */
        PDF("255044462D312E"),

        /**
         * Windows Password
         */
        PWL("E3828596"),

        /**
         * ZIP Archive
         */
        ZIP("504B0304"),

        /**
         * ARAR Archive
         */
        RAR("52617221"),

        /**
         * WAVE
         */
        WAV("57415645"),

        /**
         * AVI
         */
        AVI("41564920"),

        /**
         * Real Audio
         */
        RAM("2E7261FD"),

        /**
         * Real Media
         */
        RM("2E524D46"),

        /**
         * Quicktime
         */
        MOV("6D6F6F76"),

        /**
         * Windows Media
         */
        ASF("3026B2758E66CF11"),

        /**
         * MIDI
         */
        MID("4D546864");

        private String value = "";
        private String ext = "";

        FileType(String value) {
            this.value = value;
        }

        FileType(String value, String ext) {
            this(value);
            this.ext = ext;
        }

        public String getExt() {
            return ext;
        }

        public String getValue() {
            return value;
        }

    }



    /**
     * 上传路径
     */


    private static String getExt(String ext ){
        return  ext.substring(ext.lastIndexOf("."));
    }

    private   static  String newName(String x ){
        return UUID.randomUUID().toString().replaceAll("-","")+getExt(x);
    }


    public static  void isImage(MultipartFile file) throws IOException, FileUploadException {
        InputStream is = file.getInputStream();
        byte[] src = new byte[28];
        is.read(src, 0, 28);
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            throw new FileUploadBase.InvalidContentTypeException("当前文件类型不正确");
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v).toUpperCase();
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        FileType[] fileTypes = FileType.values();
        String b = stringBuilder.toString();
        for (FileType fileType : fileTypes) {
            if (b.startsWith("FFD8FF")||b.startsWith("89504E47")) {
                return;
            }
        }
        throw new FileUploadBase.InvalidContentTypeException("当前文件类型不正确");

    }







}

